package com.naiterui.ehp.bs.b2c.pharmacy.controller.inner;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.mvc.RequestHeaderDecode;
import com.naiterui.ehp.bs.b2c.pharmacy.bo.WarehouseBO;
import com.naiterui.ehp.bs.b2c.pharmacy.exception.PharmacyBusinessException;
import com.naiterui.ehp.bs.b2c.pharmacy.service.IWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@Api(tags = SwaggerTag.INNER_API + SwaggerTag.MEDICINE_DRUG)
@Validated
@RestController
@RequestMapping("b2c/inner/cms/warehouse")
public class WarehouseController {
    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 保存更新
     */
    @PostMapping
    @ApiOperation(value = "保存更新药店")
    public ResponseVO<Long> saveOrUpdate(@RequestBody @Valid WarehouseBO warehouseBo,
                                         @RequestHeaderDecode String operator) throws PharmacyBusinessException {
        log.info("保存更新药店 请求参数：warehouseBo = {}, operator = {}", warehouseBo, operator);
        Long id = this.warehouseService.saveOrUpdate(warehouseBo, operator);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS, id);
    }

    /**
     * 更新状态
     */
    @ApiOperation(value = "更新药店数据状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "药店ID", paramType = "path", dataType = "long", required = true),
            @ApiImplicitParam(name = "status", value = "状态:是否停用 0:启用。1:停用", paramType = "path", dataType = "int",
                    required = true)})
    @PutMapping("/{id}/{status}")
    public ResponseVO<Void> updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status, @RequestHeaderDecode String operator) {
        log.info("更新数据状态 请求参数：id = {}, status = {}, operator = {}", id, status, operator);
        this.warehouseService.updateStatus(id, status, operator);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }
}
